package com.um.jdy.platform.platform.manager;

import com.um.jdy.common.base.enums.YesOrNo;
import com.um.jdy.platform.platform.entity.dto.MessagePageDTO;
import com.um.jdy.platform.platform.entity.vo.MessageOverviewVO;
import com.um.springboot.starter.orm.entity.Response;
import com.um.springboot.starter.orm.manager.BasePlusManager;
import com.um.springboot.starter.orm.params.ListDTO;
import com.um.jdy.common.platform.service.MessageService;
import com.um.jdy.common.platform.entity.po.Message;
import com.um.jdy.common.platform.entity.dto.MessageDTO;
import com.um.jdy.common.platform.entity.vo.MessageVO;
import com.um.springboot.starter.utils.UMBeanUtils;
import org.springframework.stereotype.Component;

import java.util.List;


/**
 * 管理类：消息表
 */
@Component
public class MessageManager extends BasePlusManager<MessageService,Message,MessageDTO,MessageVO, MessagePageDTO, ListDTO> {

    /**
     * 查询消息概览
     * @return
     */
    public Response<MessageOverviewVO> getOverview(){
        List<Message> messages = service.getList(new String[]{"com_id","is_read"},new Object[]{getComId(),YesOrNo.No.name()});

        MessageOverviewVO overviewVO = new MessageOverviewVO();
        overviewVO.setMessages(UMBeanUtils.copyBeanList(messages,MessageVO.class));
        overviewVO.setNum(messages.size());
        return Response.success(overviewVO);
    }

    /**
     * 设置已读
     * @return
     */
    public Response setRead(String id){
        Message message = new Message();
        message.setPlatformMessageId(id);
        message.setIsRead(YesOrNo.Yes.name());
        service.update(message);
        return Response.success();
    }

    /**
     * 设置全部已读
     * @return
     */
    public Response setAllRead(){
        List<Message> messages = service.getList(new String[]{"com_id","is_read"},new Object[]{getComId(),YesOrNo.No.name()});
        for(Message message : messages) {
            message.setIsRead(YesOrNo.Yes.name());
            service.update(message);
        }
        return Response.success();
    }
}
